const gulp = require("gulp");
const watch = require("glob-watcher")
const client = require('scp2')
const utils = require('../utils.js')

const host = "dev:123456@192.168.51.20:";


gulp.task("estate_frontend_init", async () => {
  let srcpath = "../estate_frontend/";
  let remotepath = "/htdocs/estate_frontend";
  
  let remote = host + remotepath;
  client.scp(srcpath, remote, (err) => {
    if(err) {
      console.log(err);
    }
  });
})

gulp.task("estate_frontend", async () => {
  let srcpath = "../estate_frontend/";
  let remotepath = "/htdocs/estate_frontend";
  let watcher = watch(srcpath);
  
  watcher.on('change', function(filepath, stat) {
    console.log("change",filepath);
    let remote = utils.makepath(host, srcpath, remotepath, filepath);
    
    client.scp(filepath, remote, (err) => {
      if(err) {
        console.log(err);
      }
    });
  });
  
  watcher.on('add', function(filepath, stat) {
    console.log("add",filepath);
    let remote = utils.makepath(host, srcpath, remotepath, filepath);
    
    client.scp(filepath, remote, (err) => {
      if(err) {
        console.log(err);
      }
    });
  });
  
  watcher.on('unlink', function(path, stat) {
    console.log("unlink",path);
  });

})